﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;

namespace KasaTakip
{
    public enum ReportTypes
    {
        MagazaRaporu,
        MagazaKasaOzet,
        MagazaOzet,
        CariHesaplar,
        KrediKartiOzet,
        KrediKartiDetay,
        MagazaKontrol,
        KasiyerAciklari
    }

    public partial class frmReport : Form
    {
        public ReportTypes RaporTipi { get; set; }
        public Rapor Filtre { get; set; }

        public frmReport()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmReport_Load);
        }

        void frmReport_Load(object sender, EventArgs e)
        {
            DateTime bastarih = this.Filtre.Tarih1.ToSmallDate();
            DateTime bittarih = this.Filtre.Tarih2.ToSmallDate();

            using (KasaEntity ent = new KasaEntity())
            {
                switch (this.RaporTipi)
                {
                    case ReportTypes.MagazaRaporu:
                        var sonuc = (from c in ent.KasaKapanislari
                                     join k in ent.Kasalar on c.KasaID equals k.ID
                                     join ks in ent.Kasiyerler on c.KasiyerID equals ks.ID
                                     join m in ent.Magazalar on c.MagazaID equals m.MagazaNo
                                     where c.Tarih == bastarih
                                     select new
                                     {
                                         Magaza = m.MagazaAdi,
                                         Kasa = k.No,
                                         Kasiyer = ks.KasiyerAdi,
                                         NakitToplam = c.NakitToplam,
                                         AcikHesap = c.AcikHesapToplam,
                                         c.IadeToplam,
                                         DahiliIhtiyac = c.MasrafFaturalarToplam + c.HarcamalarToplam,
                                         KrediKarti = c.KrediToplam,
                                         ZRaporlariToplam = c.ZRaporToplam + c.FaturaToplam,
                                         TahsilatlarToplam = c.TahsilatlarToplam
                                     }).ToList();
                        gridControl1.DataSource = sonuc;
                        this.Text = "Mağaza Kasa Raporu";
                        break;
                    case ReportTypes.MagazaKasaOzet:
                        var sonuc2 = (from c in ent.KasaKapanislari
                                      join m in ent.Magazalar on c.MagazaID equals m.MagazaNo
                                      where c.Tarih == bastarih
                                      group c by new { m.MagazaAdi } into g
                                      select new
                                      {
                                          Magaza = g.Key.MagazaAdi,
                                          NakitToplam = g.Sum(x => x.NakitToplam),
                                          AcikHesap = g.Sum(x => x.AcikHesapToplam),
                                          IadeToplam = g.Sum(x => x.IadeToplam),
                                          DahiliIhtiyac = g.Sum(x => x.MasrafFaturalarToplam + x.HarcamalarToplam),
                                          KrediKarti = g.Sum(x => x.KrediToplam),
                                          ZRaporlariToplam = g.Sum(x => x.ZRaporToplam + x.FaturaToplam),
                                          TahsilatlarToplam = g.Sum(x => x.TahsilatlarToplam)
                                      }).ToList();
                        gridControl1.DataSource = sonuc2;
                        this.Text = "Mağaza Kasa Özet Raporu";
                        break;
                    case ReportTypes.MagazaOzet:
                        var sonuc3 = (from c in ent.Kapanislar
                                      join m in ent.Magazalar on c.MagazaNo equals m.MagazaNo
                                      where c.Tarih == bastarih
                                      select new
                                      {
                                          Magaza = m.MagazaAdi,
                                          DevirToplami = c.DevirToplam,
                                          c.SubedenGelen,
                                          c.BankayaYatan,
                                          c.KalanTutar,
                                          c.KalanDolar,
                                          c.KalanEuro,
                                          c.AktifDeger,
                                          c.KapasitifDeger,
                                          c.ReaktifDeger
                                      }).ToList();
                        gridControl1.DataSource = sonuc3;
                        this.Text = "Mağaza Özet Raporu";
                        break;
                    case ReportTypes.CariHesaplar:
                        var ack = (from c in ent.KasaKapanislari
                                   join a in ent.AcikHesapHareketleri on c.ID equals a.ClosureID
                                   where c.Tarih >= bastarih && c.Tarih <= bittarih
                                   select a).ToList();


                        var ths = (from k in ent.KasaKapanislari
                                   join t in ent.CariTahsilatHareketleri on k.ID equals t.ClosureID
                                   where k.Tarih >= bastarih && k.Tarih <= bittarih
                                   select t).ToList();


                        var cr = (from c in ent.Cariler
                                  join m in ent.Magazalar on c.MagazaNo equals m.MagazaNo
                                  select new
                                  {
                                      ID = c.ID,
                                      Magaza = m.MagazaAdi,
                                      CariKodu = c.Kodu,
                                      CariUnvani = c.Unvani,
                                  }).ToList();

                        var sonuc4 = (from c in cr
                                      select new
                                      {
                                          c.Magaza,
                                          c.CariKodu,
                                          c.CariUnvani,
                                          AlisverisToplami = ack.Where(x => x.CariID == c.ID).Select(x => x.KdvDahilTutar).Sum(),
                                          OdemeToplami = ths.Where(x => x.CariID == c.ID).Select(x => x.Tutar).Sum(),
                                          Bakiye = (ack.Where(x => x.CariID == c.ID).Select(x => x.KdvDahilTutar).Sum() - ths.Where(x => x.CariID == c.ID).Select(x => x.Tutar).Sum())
                                      }).ToList();

                        gridControl1.DataSource = sonuc4;
                        this.Text = "Cari Hesaplar Raporu";
                        break;
                    case ReportTypes.KrediKartiOzet:
                        var sonuc5 = (from k in ent.KasaKapanislari
                                      join m in ent.Magazalar on k.MagazaID equals m.MagazaNo
                                      join bh in ent.BankaHareketleri on k.ID equals bh.ClosureID
                                      join b in ent.Bankalar on bh.BankaID equals b.ID
                                      where k.Tarih >= bastarih && k.Tarih <= bittarih
                                      group bh by new { m.MagazaAdi, b.Kodu, b.Aciklama } into g
                                      select new
                                      {
                                          Magaza = g.Key.MagazaAdi,
                                          BankaKodu = g.Key.Kodu,
                                          BankaAdi = g.Key.Aciklama,
                                          SlipSayisi = g.Sum(x => x.Slip),
                                          Tutar = g.Sum(x => x.Tutar)
                                      }).ToList();
                        gridControl1.DataSource = sonuc5;
                        this.Text = "Banka Raporu";
                        break;
                    case ReportTypes.KrediKartiDetay:
                        var sonuc6 = (from k in ent.KasaKapanislari
                                      join ks in ent.Kasalar on k.KasaID equals ks.ID
                                      join ksy in ent.Kasiyerler on k.KasiyerID equals ksy.ID
                                      join m in ent.Magazalar on k.MagazaID equals m.MagazaNo
                                      join bh in ent.BankaHareketleri on k.ID equals bh.ClosureID
                                      join b in ent.Bankalar on bh.BankaID equals b.ID
                                      where k.Tarih >= bastarih && k.Tarih <= bittarih
                                      group bh by new { m.MagazaAdi, b.Kodu, b.Aciklama, ks.No, ksy.KasiyerAdi } into g
                                      select new
                                      {
                                          Magaza = g.Key.MagazaAdi,
                                          KasaNo = g.Key.No,
                                          KasiyerAdi = g.Key.KasiyerAdi,
                                          BankaKodu = g.Key.Kodu,
                                          BankaAdi = g.Key.Aciklama,
                                          SlipSayisi = g.Sum(x => x.Slip),
                                          Tutar = g.Sum(x => x.Tutar)
                                      }).ToList();
                        gridControl1.DataSource = sonuc6;
                        this.Text = "Banka Detay Raporu";
                        break;
                    case ReportTypes.MagazaKontrol:
                        var sonuc7 = (from c in ent.KasaKapanislari
                                      join m in ent.Magazalar on c.MagazaID equals m.MagazaNo
                                      where c.Tarih == bastarih
                                      group c by m.MagazaAdi into g
                                      select new
                                      {
                                          Magaza = g.Key,
                                          NakitToplam = g.Sum(x => x.NakitToplam),
                                          AcikHesap = g.Sum(x => x.AcikHesapToplam),
                                          IadeToplam = g.Sum(x => x.IadeToplam),
                                          DahiliIhtiyac = g.Sum(x => x.MasrafFaturalarToplam + x.HarcamalarToplam),
                                          CekToplami = g.Sum(x => ((from b in ent.Bankalar join h in ent.BankaHareketleri on b.ID equals h.BankaID where h.ClosureID == x.ID && b.BankaTipi == 2 select (double?)h.Tutar).Sum())),
                                          KrediKarti = g.Sum(x => ((from b in ent.Bankalar join h in ent.BankaHareketleri on b.ID equals h.BankaID where h.ClosureID == x.ID && b.BankaTipi == 0 select (double?)h.Tutar).Sum())),

                                          Multinet = g.Sum(x => ((from b in ent.Bankalar join h in ent.BankaHareketleri on b.ID equals h.BankaID where h.ClosureID == x.ID && b.Aciklama.StartsWith("MULTINET") && b.BankaTipi == 1 select (double?)h.Tutar).Sum())),
                                          SetKart = g.Sum(x => ((from b in ent.Bankalar join h in ent.BankaHareketleri on b.ID equals h.BankaID where h.ClosureID == x.ID && b.Aciklama.StartsWith("SET") && b.BankaTipi == 1 select (double?)h.Tutar).Sum())),
                                          VINVIN = g.Sum(x => ((from b in ent.Bankalar join h in ent.BankaHareketleri on b.ID equals h.BankaID where h.ClosureID == x.ID && b.Aciklama.StartsWith("VINVIN") && b.BankaTipi == 1 select (double?)h.Tutar).Sum())),
                                          SODEXO = g.Sum(x => ((from b in ent.Bankalar join h in ent.BankaHareketleri on b.ID equals h.BankaID where h.ClosureID == x.ID && b.Aciklama.StartsWith("SODEXO") && b.BankaTipi == 1 select (double?)h.Tutar).Sum())),
                                          PeCard = g.Sum(x => ((from b in ent.Bankalar join h in ent.BankaHareketleri on b.ID equals h.BankaID where h.ClosureID == x.ID && b.Aciklama.StartsWith("PECARD") && b.BankaTipi == 1 select (double?)h.Tutar).Sum())),
                                          Ticket = g.Sum(x => ((from b in ent.Bankalar join h in ent.BankaHareketleri on b.ID equals h.BankaID where h.ClosureID == x.ID && b.Aciklama.StartsWith("TICKET") && b.BankaTipi == 1 select (double?)h.Tutar).Sum())),

                                          ZRaporlariToplam = g.Sum(x => x.ZRaporToplam + x.FaturaToplam),
                                          TahsilatlarToplam = g.Sum(x => x.TahsilatlarToplam)
                                      }).ToList();
                        gridControl1.DataSource = sonuc7;
                        this.Text = "Mağaza Kontrol Raporu";
                        break;
                    case ReportTypes.KasiyerAciklari:
                        var sonuc8 = (from c in ent.KasaKapanislari
                                      join m in ent.Magazalar on c.MagazaID equals m.MagazaNo
                                      join k in ent.Kasiyerler on c.KasiyerID equals k.ID
                                      where c.Tarih >= bastarih && c.Tarih <= bittarih
                                      select new
                                      {
                                          c.Tarih,
                                          m.MagazaAdi,
                                          k.KasiyerAdi,
                                          c.KasaFarki
                                      }).ToList();

                        gridControl1.DataSource = sonuc8;
                        this.Text = "Kasiyer Kasa Farkları Raporu";
                        break;
                }
            }

            gridView1.ColumnPanelRowHeight = 60;
            foreach (GridColumn item in gridView1.Columns)
            {
                item.AppearanceHeader.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            }
            gridView1.BestFitColumns();
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Excel Files|*.xls";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                gridControl1.ExportToXls(dlg.FileName);
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridControl1.ShowPrintPreview();
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridControl1.Print();
        }
    }
}
